import { Nodes } from '@knno/dom';
import { FieldEventMap, FieldType, Field as UIField } from '../components/field/field';
import { JSXProps } from '@knno/dom/jsx';
import { AttrCheck, On, isBool, isNumber, isStr, jsxBuild } from './utils';
import { type InputType } from '../components/input/input';

export { UIField };

interface FieldProps extends JSXProps<UIField>, On<FieldEventMap, UIField> {
	icon?: string;
	label?: string;
	inputType?: InputType;
	type?: FieldType;
	value?: string;
	placeholder?: string;
	maxLength?: number;
	disabled?: boolean;
	checked?: boolean;
}

const ATTRS: AttrCheck = {
	icon: isStr,
	label: isStr,
	inputType: isStr,
	type: isStr,
	value: isStr,
	placeholder: isStr,
	maxLength: isNumber,
	disabled: isBool,
	checked: isBool,
};

export function Field(props: FieldProps, children: Nodes[]): UIField {
	return jsxBuild(new UIField(), props, children, ATTRS);
}
